package com.ld.security.common.rocketmq.mq;

import com.ld.security.common.core.util.JsonUtil;
import lombok.Data;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

/**
 * 生产者
 */
@Data
@Slf4j
public class SuperProducer<E> extends SuperMq<E> {


    private DefaultMQProducer producer;


    public SuperProducer(String producerGroup, String topic, String subExpression) {
        super(producerGroup, topic, subExpression);
    }

    public SuperProducer(SuperMq mq) {
        super(mq);
    }

    @SneakyThrows
    @Override
    public void run(E msg) {

        String json = JsonUtil.toJson(msg);


        Message message = new Message(this.getTopic(),this.getSubExpression(), "1",json.getBytes());
        SendResult result = producer.send(message);
        log.info("发送消息："+json);
        log.info("消息id为:  " + result.getMsgId() + "  发送状态为:" + result.getSendStatus());

    }
}
